package com.lmx.common.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Created by qin on 2016/6/27.
 */
public class URLUtil {
	private static Log logger = LogFactory.getLog(URLUtil.class);

	private Properties properties;

	private static class URLUtilInstance {
		private static URLUtil URLUtil = new URLUtil();
	}

	public static URLUtil getInstance() {
		return URLUtilInstance.URLUtil;
	}

	/**
	 * 初始化方法
	 */
	public URLUtil() {
		try {
			properties = PropertiesFileUtil
					.getProperties("application.properties");
		} catch (Exception e) {
			logger.debug("配置文件获取异常！" + e.toString());
			// e.printStackTrace();
		}
	}

	/**
	 * 获取根目录
	 */
	public String getRootDir() {
		String rootDir = "";
		rootDir = properties.getProperty("url.root");
		return rootDir;
	}

	/**
	 * 获取文件类型目录、一级子目录
	 */
	public String getFileTypeDir(String contentType) {
		String fileTypeDir = properties.getProperty("url.fileType.default");
		if (contentType != null && contentType.startsWith("image")) {
			fileTypeDir = properties.getProperty("url.fileType.image");
		}
		if (contentType != null && contentType.startsWith("audio")) {
			fileTypeDir = properties.getProperty("url.fileType.audio");
		}
		if (contentType != null && contentType.startsWith("zipPackage")) {
			fileTypeDir = properties.getProperty("url.fileType.zip");
		}
		if (contentType != null && contentType.startsWith("media")) {
			fileTypeDir = properties.getProperty("url.fileType.media");
		}
		logger.debug("fileTypeDir : " + fileTypeDir);
		return fileTypeDir;
	}

	/**
	 * 获取业务类型目录、二级子目录
	 */
	public String getBusinessTypeDir(String businessType) {
		String businessTypeDir = "";
		if ("headImg".equals(businessType)) {
			businessTypeDir = properties
					.getProperty("url.businessType.headImg");
		} else if ("idcardImg".equals(businessType)) {
			businessTypeDir = properties
					.getProperty("url.businessType.idcardImg");
		} else if ("qualificationImg".equals(businessType)) {
			businessTypeDir = properties
					.getProperty("url.businessType.qualificationImg");
		} else if ("callRecord".equals(businessType)) {
			businessTypeDir = properties
					.getProperty("url.businessType.callRecord");
		} else if ("iconFile".equals(businessType)) {
			businessTypeDir = properties
					.getProperty("url.businessType.iconFile");
		} else if ("phoneNumAuth".equals(businessType)) {
			businessTypeDir = properties
					.getProperty("url.businessType.phoneAuth");
		} else if ("QRimg".equals(businessType)) {
			businessTypeDir = properties.getProperty("url.businessType.QRimg");
		} else if ("editorImg".equals(businessType)) {
			businessTypeDir = properties
					.getProperty("url.businessType.editorImg");
		} else if ("media".equals(businessType)) {
			businessTypeDir = properties.getProperty("url.businessType.media");
		} else if ("documentFile".equals(businessType)) {
			businessTypeDir = properties
					.getProperty("url.businessType.documentFile");
		} else {
			businessTypeDir = businessType + "/";
		}

		return businessTypeDir;
	}

	/**
	 * 获取日期目录、 三级目录
	 */
	public String getDateDir() {
		String dateDir = "";
		Date now = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		dateDir = sdf.format(now) + "/";
		return dateDir;
	}

	/**
	 * 获取文件相对路径
	 *
	 * @param name
	 * @return path
	 */
	// TODO 能不能换个方法名字和注释,这不是相对路径
	public String getRelativePath(String name) {
		String path = "";
		path = /*
				 * getFileTypeDir(contentType) +
				 * getBusinessTypeDir(businessType) +
				 */getDateDir() + name;
		return path;
	}

	/**
	 * 获取相对路径
	 *
	 * @author ytche
	 * @param contentType
	 * @param businessType
	 * @param name
	 * @return
	 * @date 2017年8月16日 上午11:19:38
	 */
	public String getRelativePath(String contentType, String businessType,
			String name) {
		return getFileTypeDir(contentType) + getBusinessTypeDir(businessType)
				+ getDateDir() + name;
	}

	/**
	 * 获取文件完整路径
	 *
	 * @param contentType
	 * @param businessType
	 * @param name
	 * @return
	 */
	public String getFullPath(String contentType, String businessType,
			String name) {
		String path = "";
		path = getRootDir() + getFileTypeDir(contentType)
				+ getBusinessTypeDir(businessType) + getDateDir() + name;
		return path;
	}

	public String getSaveDirPath(String contentType, String businessType) {
		return getRootDir() + getFileTypeDir(contentType)
				+ getBusinessTypeDir(businessType) + getDateDir();
	}

	/**
	 * 获取图片服务器路径
	 *
	 * @return
	 */
	public String getImgPath(String businessType) {
		String businessTypeDir = getBusinessTypeDir(businessType);
		return this.getImgServerUrl() + "/" + businessTypeDir;
	}

	/**
	 * 获取录音存储路径
	 */
	public String getRecordPath() {
		String fileTypeDir = properties.getProperty("url.fileType.audio");
		String path = getRootDir() + fileTypeDir;
		return path;
	}

	/**
	 * 获取二维码存储路径
	 */
	public String getQRPath() {
		String fileTypeDir = properties.getProperty("url.fileType.image");
		String businessTypeDir = properties
				.getProperty("url.businessType.QRimg");
		String path = getRootDir() + fileTypeDir + businessTypeDir
				+ getDateDir();
		return path;
	}

	/**
	 * 获取资源文件地址
	 */
	public String getResource() {
		String resource = "";
		resource = properties.getProperty("url.resource") + "/audio/";
		return resource;
	}

	/**
	 * 获取资源服务器地址
	 *
	 * @author ytche
	 * @return
	 * @date 2017年8月16日 上午11:16:39
	 */
	public String getResourceServerUrl() {
		String resource = "";
		resource = properties.getProperty("resource.server.url") + "/";
		return resource;
	}

	/**
	 * 获取图片服务器地址
	 *
	 * @author ytche
	 * @return
	 * @date 2017年8月16日 上午11:16:16
	 */
	public String getImgServerUrl() {
		return properties.getProperty("img.server.url");
	}

	/**
	 * 根据name获取property文件中对应的值
	 *
	 * @author ytche
	 * @param name
	 * @return
	 * @date 2017年5月10日 下午1:46:55
	 */
	public String getValueByName(String name) {
		String value = "";
		value = properties.getProperty(name);
		return value;
	}

	public static void main(String[] args) {
		URLUtil URLUtil = new URLUtil();
		System.out
				.println(URLUtil.getFullPath("image/jpg", "test", "test.jpg"));
	}

	/**
	 * 客户端地址
	 *
	 * @author ytche
	 * @return
	 * @date 2017年8月10日 下午6:05:52
	 */
	public String getClintUrl() {
		return properties.getProperty("clint.url");
	}

}
